package com.bysj.money_diary;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Base64;
import android.widget.Toast;

@SuppressLint("ShowToast") 
public class ImageUtil {
	/**
	 * 锟斤拷Bitmap图片转锟斤拷为base64 锟斤拷锟斤拷亩锟斤拷锟斤拷锟酵计拷锟斤拷
	 * 
	 * @param myImage
	 */
	public static String getBase64ImageEncode(Bitmap myImage) {
		Bitmap bmSmall = Bitmap.createBitmap(myImage, 0, 0, myImage.getWidth(), myImage.getHeight());
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		bmSmall.compress(Bitmap.CompressFormat.JPEG, 100, stream);
		byte[] arrays = stream.toByteArray();
		// base64 encode
		byte[] encode = Base64.encode(arrays, Base64.DEFAULT);
		String base64Encode = new String(encode);
		return base64Encode;
	}

	/**
	 * @param 锟斤拷锟斤拷图片
	 * @param path
	 *            图片路锟斤拷
	 * @return Bitmap
	 */
	public static Bitmap getScalingPhoto(String path) {
		// 锟斤拷锟斤拷图片时锟斤拷要使锟矫碉拷锟侥诧拷锟斤拷锟阶帮拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
		Options options = new Options();
		// 锟斤拷为锟斤拷锟斤拷锟斤拷锟斤拷锟节存，只锟斤拷取图片锟斤拷锟�
		options.inJustDecodeBounds = true;
		// 锟斤拷锟铰凤拷锟斤拷锟饺⊥计�
		BitmapFactory.decodeFile(path, options);
		// 锟斤拷锟斤拷锟斤拷锟脚憋拷锟斤拷
		double ratio = Math.max(options.outWidth * 1.0d / 1024f, options.outHeight * 1.0d / 1024f);
		options.inSampleSize = (int) Math.ceil(ratio);
		// 锟斤拷锟斤拷锟节存，只锟斤拷取图片锟斤拷锟�
		options.inJustDecodeBounds = false;
		// 锟斤拷锟铰斤拷锟斤拷图片
		Bitmap photoImage = BitmapFactory.decodeFile(path, options);
		// 锟斤拷锟截斤拷锟斤拷图片锟矫碉拷锟斤拷值
		return photoImage;
	}

	/**
	 * 锟斤拷取锟斤拷锟斤拷锟酵计拷锟铰凤拷锟�
	 * 
	 * @param context
	 *            锟斤拷锟斤拷锟斤拷
	 * @param data
	 *            onActivityResult锟截碉拷锟斤拷锟斤拷锟叫碉拷Intent锟斤拷锟�
	 */
	public static String getPhotoPath(Context context, Intent data) {
		if (data != null) {
			// 锟矫碉拷图片锟斤拷Uri
			Uri uri = data.getData();
			// 通锟斤拷锟斤拷锟捷斤拷锟斤拷锟斤拷锟斤拷锟酵计�
			Cursor query = context.getContentResolver().query(uri, null, null, null, null);
			query.moveToFirst();
			// 锟矫碉拷图片锟斤拷锟斤拷锟斤拷锟斤拷
			int idx = query.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
			// 锟矫碉拷图片锟斤拷路锟斤拷
			String photoStr = query.getString(idx);
			query.close();
			return photoStr;
		} else {
			Toast.makeText(context, "Try Again", 0).show();
			return null;
		}
	}
	
	
	/**
	 * 锟斤拷装锟斤拷锟斤拷锟斤拷息
	 */
	public static Map<String, Double> GetMapGesture(JSONObject gesture) {
		Map<String, Double> mapGesture = new HashMap<String, Double>();
		try {
			mapGesture.put("unknown", gesture.getDouble("unknown"));
			mapGesture.put("heart_a", gesture.getDouble("heart_a"));
			mapGesture.put("heart_b", gesture.getDouble("heart_b"));
			mapGesture.put("heart_c", gesture.getDouble("heart_c"));
			mapGesture.put("heart_d", gesture.getDouble("heart_d"));
			mapGesture.put("ok", gesture.getDouble("ok"));
			mapGesture.put("hand_open", gesture.getDouble("hand_open"));
			mapGesture.put("thumb_up", gesture.getDouble("thumb_up"));
			mapGesture.put("thumb_down", gesture.getDouble("thumb_down"));
			mapGesture.put("rock", gesture.getDouble("rock"));
			mapGesture.put("namaste", gesture.getDouble("namaste"));
			mapGesture.put("palm_up", gesture.getDouble("palm_up"));
			mapGesture.put("fist", gesture.getDouble("fist"));
			mapGesture.put("index_finger_up", gesture.getDouble("index_finger_up"));
			mapGesture.put("double_finger_up", gesture.getDouble("double_finger_up"));
			mapGesture.put("victory", gesture.getDouble("victory"));
			mapGesture.put("big_v", gesture.getDouble("big_v"));
			mapGesture.put("phonecall", gesture.getDouble("phonecall"));
			mapGesture.put("beg", gesture.getDouble("beg"));
			mapGesture.put("thanks", gesture.getDouble("thanks"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return mapGesture;
	}

	/**
	 * 锟斤拷取map锟斤拷锟较碉拷锟斤拷锟絭alue锟侥硷拷 锟斤拷取锟斤拷前锟斤拷锟斤拷锟斤拷锟斤拷息 (英锟斤拷)
	 * 
	 * @param Map<String, Double> map
	 */
	public static String getGestureUS(Map<String, Double> map) {
		Double max = 0.0;
		String result = null;
		for (Entry<String, Double> entry : map.entrySet()) {
			if (entry.getValue() > max) {
				result = entry.getKey();
				if (result != null)
					max = entry.getValue();
			}
		}
		return result;
	}

	/**
	 * 锟斤拷锟截碉拷前锟斤拷锟斤拷锟斤拷锟斤拷息(锟斤拷锟斤拷)
	 * 
	 * @param String str
	 */
	public static String getGestureENS(String str) {
		String gesture = null;
		if (str.equals("unknown")) {
			gesture = "UNKNOWN";
		} else if (str.equals("heart_a")) {
			gesture = "A";
		} else if (str.equals("heart_b")) {
			gesture = "B";
		} else if (str.equals("heart_c")) {
			gesture = "C";
		} else if (str.equals("heart_d")) {
			gesture = "D";
		} else if (str.equals("ok")) {
			gesture = "OK";
		} else if (str.equals("hand_open")) {
			gesture = "open";
		} else if (str.equals("thumb_up")) {
			gesture = "up";
		} else if (str.equals("thumb_down")) {
			gesture = "down";
		} else if (str.equals("rock")) {
			gesture = "ROCK";
		} else if (str.equals("namaste")) {
			gesture = "namaste";
		} else if (str.equals("palm_up")) {
			gesture = "palm_up";
		} else if (str.equals("fist")) {
			gesture = "fist";
		} else if (str.equals("index_finger_up")) {
			gesture = "index_finger_up";
		} else if (str.equals("double_finger_up")) {
			gesture = "double_finger_up";
		} else if (str.equals("victory")) {
			gesture = "victory";
		} else if (str.equals("big_v")) {
			gesture = "big_v";
		} else if (str.equals("phonecall")) {
			gesture = "phonecall";
		} else if (str.equals("beg")) {
			gesture = "beg";
		} else if (str.equals("thanks")) {
			gesture = "thanks";
		}
		return gesture;
	}
	
	//浜鸿劯璇嗗埆-鎬у埆
	public static String getFaceGender(String str){
		String gender="";
		if(str.equals("Male")){
			gender="男";
		}else if(str.equals("Female")){
			gender="女";
		}else{
			gender="";
		}
		return gender;
	}
	
//	public static String getSearchSearch(int search){
//		String res="";
//		if(search<=20){
//			res="鏄悓涓�涓汉鍙兘鎬ф瀬浣�";
//		}else if(search <=40){
//			res="鏄悓涓�涓汉鍙兘鎬т綆";
//		}else if(search <=60){
//			res="鏈夊彲鑳芥槸鍚屼竴涓汉";
//		}else if(search <=80){
//			res="寰堝彲鑳芥槸鍚屼竴涓汉";
//		}else{
//			res="鏋佹湁鍙兘鏄悓涓�涓汉";
//		}
//		return res;
//	}
}
